package ru.gelin.android.bells;

import java.util.ArrayList;
import java.util.List;

import ru.gelin.android.bells.timers.Timer;
import android.content.Context;
import android.media.Ringtone;
import android.media.RingtoneManager;
import android.net.Uri;
import android.util.Log;

/**
 *  The list of ringtones to be played on alarm.
 *  It is populated from the list of alarmed timers. The set of different
 *  alarm ringtones is created. The first ringtone is started by {@link #playNext()}
 *  method call.
 *  The next ringtone is tried to start by {@link #playNext()} call. If the
 *  previous ringtone is playing, nothing is happened. If the previous ringtone
 *  is stopped, the next ringtone is started.
 *  After the last, the first ringtone will be played again.
 */
public class RingtoneList implements Constants {

    /** Set of ringtone URIs */
    List<Uri> ringtones = new ArrayList<Uri>();
    /** Current application context */
    Context context;
    /** Ringtone manager to play a ringtone */
    RingtoneManager ringtoneManager;
    /** Current playing ringtone */
    Ringtone ringtone;
    /** Next ringtone index */
    int nextRingtone = 0;
    
    /**
     *  Constructs the list
     *  @param  context to get ringtone manager
     *  @param  list of alarmed timers
     */
    public RingtoneList(Context context, List<Timer> timers) {
        this.context = context;
        
        ringtoneManager = new RingtoneManager(context);
        ringtoneManager.setType(RingtoneManager.TYPE_ALARM);
        //ringtoneManager.setStopPreviousRingtone(true);
        
        for (Timer timer : timers) {
            String alarm = timer.getAlarm();
            if (!ringtones.contains(alarm)) {
                try {
                    ringtones.add(Uri.parse(alarm));
                } catch (Exception e) {
                    Log.w(TAG, "invalid alarm: " + alarm);
                }
            }
        }
        
        if (ringtones.size() == 0) {
            ringtones.add(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM));
        }
    }
    
    /**
     *  Starts playing of the next (or the first) ringtone.
     */
    public void playNext() {
        if (ringtone == null || !ringtone.isPlaying()) {
            ringtone = getNextRingtone();
            ringtone.play();
        }
    }
    
    /**
     *  Stops playing of the currect ringtone.
     */
    public void stop() {
        if (ringtone != null && ringtone.isPlaying()) {
            ringtone.stop();
        }
    }
    
    /**
     *  Returns the next ringtone.
     */
    Ringtone getNextRingtone() {
        Uri uri = ringtones.get(nextRingtone);
        int position = ringtoneManager.getRingtonePosition(uri);
        if (position < 0) {
            position = 0;
        }
        Ringtone ringtone = ringtoneManager.getRingtone(position);
        Log.d(TAG, "playing " + uri + " - " + ringtone.getTitle(context));
        nextRingtone++;
        if (nextRingtone >= ringtones.size()) {
            nextRingtone = 0;
        }
        return ringtone;
    }
    
}
